package com.shanzmoo.pay.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shanzmoo.pay.domain.request.merchantRequest.SaobeiMerchantAddRequest;
import com.shanzmoo.pay.domain.request.merchantRequest.SaobeiMerchantCheckRequest;
import com.shanzmoo.pay.domain.response.merchantResponse.SaobeiMerchantAddResponse;
import com.shanzmoo.pay.domain.response.merchantResponse.SaobeiMerchantCheckResponse;
import com.shanzmoo.pay.util.ApiConstants;
import com.shanzmoo.pay.util.ApiUtil;
import com.shanzmoo.pay.util.HttpClientUtils;
import com.shanzmoo.pay.util.RandomUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author dingmeng
 * @title: MerchantManager
 * @projectName jyf-mall-server
 * @description: 扫呗--商户管理
 * @date 2021/4/415:26
 */
@Slf4j
@Component
public class SaobeiMerchantManager {
    @Value("${saobei.merchant.notifyUrl}")
    private String notifyUrl;
    /**
     * 商户名称验重
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiMerchantCheckResponse checkMerchantName(SaobeiMerchantCheckRequest request) {
        //发送请求
        log.info("商户名称验重请求报文" + JSON.toJSONString(request));
        JSONObject obj = ApiUtil.convertMerchantRequest(request, request, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/merchant/200/checkname", obj.toJSONString());
        SaobeiMerchantCheckResponse response = JSON.parseObject(xmlText, SaobeiMerchantCheckResponse.class);
        log.info("商户名称验重返回报文:" + JSON.toJSONString(response));
        return response;

    }

    /**
     * 创建商户
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiMerchantAddResponse addMerchant(SaobeiMerchantAddRequest request) {
        request.setMerchant_type("2");
        request.setParent_no(ApiConstants.MERCHANT_PARENT_NO);
        request.setNotify_url(notifyUrl);
        //发送请求
        log.info("创建商户请求报文:" + JSON.toJSONString(request));
        JSONObject obj = ApiUtil.convertMerchantRequest(request, request, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/merchant/200/add", obj.toJSONString());
        SaobeiMerchantAddResponse response = JSON.parseObject(xmlText, SaobeiMerchantAddResponse.class);
        log.info("创建商户返回报文:" + JSON.toJSONString(response));
        return response;

    }

    public static void main(String[] args) throws Exception {
        //商户名称验重requst
//        SaobeiMerchantCheckRequest request = new SaobeiMerchantCheckRequest();
//        request.setInst_no(ApiConstants.INST_NO);
//        request.setMerchant_name("个体户桂朝");
//        request.setTrace_no(RandomUtils.getRandom(32));
//        checkMerchantName(request);
        //创建商户requst
//        SaobeiMerchantAddRequest request = new SaobeiMerchantAddRequest();
//        request.setInst_no(ApiConstants.INST_NO);
//        request.setTrace_no(RandomUtils.getRandom(32));
//        request.setMerchant_name("测试创建商户");
//        request.setMerchant_alias("测试创建商户");
//        request.setMerchant_company("赢想未来智能科技(武汉)有限公司");
//        request.setMerchant_province("湖北省");
//        request.setMerchant_province_code("420");
//        request.setMerchant_city("武汉市");
//        request.setMerchant_city_code("5210");
//        request.setMerchant_county("武昌区");
//        request.setMerchant_county_code("5215");
//        request.setMerchant_address("湖北省武汉市 武昌区 雄楚大道1758号");
//        request.setMerchant_person("桂朝");
//        request.setMerchant_phone("13554393999");
//        request.setMerchant_email("2373856453@qq.com");
//        request.setBusiness_name("各类杂货店、便利店");
//        request.setBusiness_code("5139");
//        request.setMerchant_business_type("1");
//        request.setSettlement_type("1");
//        request.setLicense_type("0");
//        request.setAccount_name("胡胜祥/赢想未来智能科技(武汉)有限公司");
//        request.setAccount_no("3202003909200190229");
//        request.setAccount_phone("13554399999");
//        request.setAccount_type("1");
//        request.setRate_code("M0030");
//        request.setArtif_nm("胡胜祥");
//        request.setBank_name("中国工商银行股份有限公司武汉中山路支行");
//        request.setBank_no("102521002767");
//        request.setImg_license("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setImg_idcard_a("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setImg_idcard_b("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setImg_logo("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setImg_indoor("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1g");
//        request.setImg_open_permits("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setImg_org_code("http://help.lcsw.cn/xrmpic/ddodxdb6bi3gwwps/wakc5p#title-node1");
//        request.setSettle_type("1");
//        request.setMerchant_id_no("421125199210191718");
//        addMerchant(request);
    }
}
